package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetBirthdayVip extends RESTBase {
    private final static String RANGE = "10";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetBirthdayVip(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Vip> getData(String empId, String type, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,type,page), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
//        String count = new JSONArray(str).getJSONObject(0).getString("count");
//        HashMap map = new HashMap();
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vips= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length() ; i++) {
                Vip vip = new Vip();
                vip.setVipName(jsonArray.getJSONArray(i).getString(0));
                vip.setSex(jsonArray.getJSONArray(i).getString(1));
                vip.setPhone(jsonArray.getJSONArray(i).getString(2));
                vip.setCardNo(jsonArray.getJSONArray(i).getString(3));
                vip.setBirthday(jsonArray.getJSONArray(i).getString(4));
                vip.setIntegral(jsonArray.getJSONArray(i).getString(5));
                vip.setTotAmtActual(jsonArray.getJSONArray(i).getString(6));
                //最近消费日
                if("null".equals(jsonArray.getJSONArray(i).getString(7))){
                    vip.setLastDate("暂无");
                }else {
                    vip.setLastDate(jsonArray.getJSONArray(i).getString(7));
                }
                vip.setAvgDiscount(jsonArray.getJSONArray(i).getString(8));
                vip.setId(jsonArray.getJSONArray(i).getString(9));
                vip.setRecentDate(jsonArray.getJSONArray(i).getString(10));
                vip.setEmpName(jsonArray.getJSONArray(i).getString(11));
                vip.setBJ(jsonArray.getJSONArray(i).getString(12));
                vip.setOpenid(jsonArray.getJSONArray(i).getString(13));
                vip.setRank(jsonArray.getJSONArray(i).getString(14));
                if("null".equals(jsonArray.getJSONArray(i).getString(15))){
                    vip.setLogoImg("http://lfwd.cloudsmicro.com:8080/img/EnterpriseWeChat/vip/app.png");
                }else {
                    vip.setLogoImg(jsonArray.getJSONArray(i).getString(15));
                }
                vip.setExternalUserId(jsonArray.getJSONArray(i).getString(16));
                vips.add(vip);
            }
        }

        System.err.println(vips.size());
        return vips;
    }

    private MultiValueMap Data(String empId,String type,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'C_VIP_ID;VIPNAME'");//名字
        colums.add("'C_VIP_ID;SEX'");//性别
        colums.add("'C_VIP_ID;MOBIL'");//手机号
        colums.add("'C_VIP_ID;CARDNO'");//卡号
        colums.add("'C_VIP_ID;BIRTHDAY'");//生日
        colums.add("'C_VIP_ID;FA_VIPACC_ID;INTEGRAL'");//积分
        colums.add("'C_VIP_ID;FA_VIPACC_ID;TOT_AMT_ACTUAL'");//总消费
        colums.add("'C_VIP_ID;FA_VIPACC_ID;LASTDATE'");//最近消费日
        colums.add("'C_VIP_ID;FA_VIPACC_ID;AVG_DISCOUNT'");//平均折扣
        colums.add("'C_VIP_ID'");//平均折扣
        colums.add("'C_VIP_ID;RECENTDATE'");//最近邀约日期
        colums.add("'HR_EMPLOYEE_ID1;NAME'");//导购
        colums.add("BJ");//回访标记
        colums.add("'C_VIP_ID;OPENID2'");
//        colums.add("'C_VIP_ID;SEX'");
        colums.add("'C_VIP_ID;RANK'");
        colums.add("'C_VIP_ID;LOGO_URL'");
        colums.add("'C_VIP_ID;EXTERNALUSERID'");//企业微信我的顾客标识

        params.put("columns", colums);
        params.put("table", "C_VIP_MONTHRVISIT");//表名
//        params.put("count", "true");
        params.put("range",RANGE);
//        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap orderby = new HashMap();

        orderby.put("column", "DAY_BIRTH");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'HR_EMPLOYEE_ID1'");
        String ac = "'"+empId+"'";
        expr1.put("condition",ac);

        HashMap expr2 = new HashMap();
        expr2.put("column","BJ");
        String state= null;
        if(type.equals("1")){
            state="'未回访'";
        }else if(type.equals("2")){
            state="'已回访'";
        }

        expr2.put("condition",state);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetBirthdayVip g = new GetBirthdayVip("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("5907","1",2));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
